package com.bot.nft.module.mb.api;


import com.bot.auth.core.pojo.SaBaseClientLoginUser;
import com.bot.auth.core.util.StpClientLoginUserUtil;
import com.bot.common.pojo.CommonResult;
import com.bot.nft.module.mb.controller.MemberController;
import com.bot.nft.module.mb.domain.bo.LoginQueryBo;
import com.bot.nft.module.mb.domain.bo.MbUserInfoBo;
import com.bot.nft.module.mb.domain.vo.MbUserInfoVo;
import com.bot.nft.module.mb.service.IMbMemberService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * 描述: 会员uniApp控制层
 * <p>
 * 时间: 2024-01-10 20:14
 * 负责人: 卢梦丽
 **/

@CrossOrigin
@RestController
@RequestMapping("/app/mb")

public class AppMemberController extends MemberController {

    @Resource
    private IMbMemberService iMbMemberService;


    /**
     * 查看会员
     *
     * @return memberMbInfo
     */
    @GetMapping("/getMbInfo.do")
    public CommonResult<MbUserInfoVo> ApiGetMemberMbInfo() {
        SaBaseClientLoginUser clientLoginUser = StpClientLoginUserUtil.getClientLoginUser();
        String id = clientLoginUser.getId();
        MbUserInfoVo mbInfoUniApp = iMbMemberService.getMbInfoUniApp(id);
        return CommonResult.data(mbInfoUniApp);

    }

    /**
     * 设置用户密码uniApp
     *
     * @return
     */
    @PostMapping("/setMbPwd.do")
    public CommonResult<Boolean> addMemberMbInfo(@RequestBody MbUserInfoBo mbUserInfoBo) {
        SaBaseClientLoginUser clientLoginUser = StpClientLoginUserUtil.getClientLoginUser();
        String id = clientLoginUser.getId();
        String password = mbUserInfoBo.getPassword();
        Boolean addPwdUniApp = iMbMemberService.addPwdUniApp(id, password);
        if (!addPwdUniApp) {
            return CommonResult.error();
        }

        return CommonResult.ok();
    }

    /**
     * 修改昵称
     *
     * @return
     */
    @PostMapping("/{nickname}/updateNick.do")
    public CommonResult<Boolean> updateNick(@PathVariable("nickname")String nickname) {
        SaBaseClientLoginUser clientLoginUser = StpClientLoginUserUtil.getClientLoginUser();
        String id = clientLoginUser.getId();

        Boolean updateNick = iMbMemberService.updateNick(id, nickname);
        if (!updateNick) {
            return CommonResult.error();
        }

        return CommonResult.ok();
    }

    /**
     * 修改头像功能
     * @param mbMemberInfoBo
     * @return
     */
    @PostMapping("/updateAvatar.do")
    public CommonResult<Boolean> updateAvatar(@RequestBody MbUserInfoBo mbMemberInfoBo){
        SaBaseClientLoginUser clientLoginUser = StpClientLoginUserUtil.getClientLoginUser();
        String id = clientLoginUser.getId();
        String avatar = mbMemberInfoBo.getAvatar();
        String uuidAvatar = mbMemberInfoBo.getUuidAvatar();
        Boolean updateAvatar = iMbMemberService.updateAvatar(id, avatar, uuidAvatar);
        if (!updateAvatar) {
            return CommonResult.error();
        }

        return CommonResult.ok();
    }

    /**
     * 手机号验证
     *
     * @param phone 手机号
     * @return
     */
    @GetMapping("/sendcode.do")
    public CommonResult<Boolean> sendcode(String phone) {
        Boolean sendcode = iMbMemberService.sendcode(phone);
        if (!sendcode) {
            return CommonResult.error("验证码异常");
        }
        return CommonResult.ok();
    }

    /**
     * 会员登录功能
     *
     * @param loginQueryBo 登录信息
     * @return 令牌
     */
    @PostMapping("/mbLogin.do")
    private CommonResult<String> loginAccountMbInfo(@RequestBody LoginQueryBo loginQueryBo) {
        //调用app端登录接口
        String token = iMbMemberService.mbLogin(loginQueryBo);
        return CommonResult.data(token);
    }


    @GetMapping("/getMemberApp.do")
    public CommonResult<MbUserInfoVo> getMemberMbInfo() {
        //获取当前C端登录用户信息
        SaBaseClientLoginUser clientLoginUser = StpClientLoginUserUtil.getClientLoginUser();
        //获取用户标识
        String id = clientLoginUser.getId();
        MbUserInfoVo memberMbInfo = iMbMemberService.getMemberMbInfo(id);
        return CommonResult.data(memberMbInfo);
    }
}
